$(function(){
    var currentCity=SYS_GLOBAL_CONFIG.getCurrentCity("currentCity");
    var url=SYS_GLOBAL_CONFIG.reqUrl,
    async="true",
    type="post";
    var l_s;
    if(window.localStorage){
        l_s=window.localStorage;      
    }else{
        l_s={};
    }
    //点击搜索框，进行搜索
    $("#search-input").on("click",function(){
        l_s.search_state=1;
        location.href="./searchGoods.html?currentCity="+currentCity;
    });
    //切换不同的页面
    $(".c-ul li").on("click",function(){
        var links=$(this).find("a").attr("data-link");
        location.href="./"+links+".html?currentCity="+currentCity;
    });
    var address=currentCity;//获取当前城市
    var currentPage=1;//默认设置当前页为第一页
    var totalPage=1;//设置默认总页数
    var show_count=5;//设置默认每次加载的条数
    
    var dataProduct=JSON.stringify({//根据address获取附近产品
        "code":"getProductList",
        "version":"1.0",
        "data":{
            "areaId":"",
            "parentPdTypeId":0,
            "userId":0,
            "address":address,
            "currentPage":currentPage,
            "showCount":show_count,
            "productName":"",
            "productTypeId":0
        }
    });
    console.log("请求参数"+dataProduct);
    //获取全部的类型
    var allProduct=JSON.stringify({
        "code":"getPdOrServTypeList",
        "version":"1.0",
        "data":{
            "parentServiceId":0,
            "type":"PRODUCT",
            "parentPdTypeId":0,
        }
    });
    function getData(url,requestData,call_func,async,type){
        console.log("请求参数"+requestData);
            $.ajax({
                url:url,
                type:type,
                datatype:"json",
                contentType:"application/json",
                data:requestData,
                async:async,
                success:function(data){
                    call_func(data);  
                },
                error:function(msg){
                    location.href="./error.html";
                }   
            });        
        }
    /**
     *获取产品列信息
     **/
    function call_func1(data){
        var datas=data.data.datas;
        var lens=datas.length;
        totalPage=data.data.totalPage;//总页数
        var dataStr1="";
        var dataStr2="";
        var arr1=[];
        var arr2=[];
        if(lens>0){
           for(var i=0;i<lens;i++){
                if(datas[i].sellerFees.length>0){
                    if(datas[i].sellerFees[0].isAvaliable==1){
                        arr2.push(datas[i]);    
                    }else{
                        arr1.push(datas[i]);
                    }
                }else{
                    arr1.push(datas[i]);
                }
           }
        }
        if(arr1.length>0){
            for(var i=0,len=arr1.length;i<len;i++){
               var isauth="";
               if(arr1[i].seller.isSellerAuth==0){
                   isauth='<span>(未认证)</span>';
               }else{
                   isauth='<img src="./images/vip_icon.png" alt="">';
               }
               dataStr1+='<div data-proId="'+arr1[i].productId+'" class="zwl-noCertificate"><div class="goodNoCertificate"><img class="zwl-img" src="'+arr1[i].mainImgPath+'" alt=""><div class="zwl-shoppingDetail"><p class="zwl-p">'+arr1[i].productName+'</p><span class="shop1">'+arr1[i].productTypeName+'</span><span class="shopRe1">'+isauth+'</span><span class="span2"> <span class="money">'+arr1[i].saleprice+'元/</span><span class="cess">'+arr1[i].unit+'</span></span></div></div></div>';

           }
       }
        var downS='';
        if(arr2.length>0){
            for(var i=0,len=arr2.length;i<len;i++){
               var isauth="";
               if(arr2[i].seller.isSellerAuth==0){
                   isauth='<span>(未认证)</span>';
               }else{
                   isauth='<img src="./images/vip_icon.png" alt="">';
               }
                var main_img='';
                var k_arr=arr2[i].productImgs;
                var k_len=k_arr.length;
                if(k_len>0){
                    for(var k=0;k<k_len;k++){
                        if(k===3){
                            break;    
                        }
                        main_img+='<img src="'+k_arr[k].FULLPATH+'" alt="">';    
                    }    
                }
                var t_len=arr2[i].tagServices.length;
                var arrTag=arr2[i].tagServices;
                if(t_len>2){
                    downS='<i class="down"></i>';
                }
                var tags='';
                var tagStr='';
                for(var t=0;t<t_len;t++){
                    tagStr+='<p class="tags_info"><img src="'+arrTag[t].tagIconPath+'" alt=""><span>'+arrTag[t].userRemark+'</span></p>';
                    if(t<3){
                        tags+='<span>'+arrTag[t].name+'</span>'; 
                    }
                }
                dataStr2+='<div class="zwl-Certificate"><div class="zwl-show" data-proId="'+arr2[i].productId+'"><div class="goodNoCertificate"><img class="zwl-img" src="'+arr2[i].mainImgPath+'" alt=""><div class="zwl-shoppingDetail"><p class="zwl-p">'+arr2[i].productName+'</p><p class="zwl-way">'+tags+'</p><span class="shop">'+arr2[i].productTypeName+'</span><span class="shopRe">'+isauth+'</span><span class="span1"><span class="money">'+arr2[i].saleprice+'元/</span><span class="cess">'+arr2[i].unit+'</span></span></div></div><div class="zwl-pic">'+main_img+'<img src="'+arr2[i].mainImgPath+'" alt=""></div></div><div class="zwl-benefit max2_h">'+tagStr+downS+'</div></div>';
           }
       }
        $("#goods-lists").append(dataStr1+""+dataStr2);
        var down_flag=1;
        if(downS!=''){
            $(".zwl-benefit i").off("click");
            $(".zwl-benefit i").on("click",function(){
                if(down_flag==1){  
                    $(this).addClass("up").removeClass("down").parent(".zwl-benefit").addClass("auto_h").removeClass("max2_h");
                    down_flag=0;
                }else{
                    $(this).addClass("down").removeClass("up").parent(".zwl-benefit").addClass("max2_h").removeClass("auto_h");
                    down_flag=1;
                }
            });    
        }
        $(".zwl-noCertificate,.zwl-show").on("click",function(){
            var proId=$(this).attr("data-proId");
            location.href="./goodDetail.html?productId="+proId;
        });
   }
    /**
     *获取全部产品类
     **/
    function call_func2(data){
        console.log(data);
        var list=data.data.productTypes;
        var listLen=list.length;
        var count=Math.ceil(listLen/12);//页数
        var dataStr="";
        var li="";
        for(var j=0;j<count;j++){
            var dataS="";
            for(var i=j*12;i<(j+1)*12;i++){
                if(i==listLen){
                    break;
                }
                dataS+='  <li class="zwl-li" data-typeId="'+list[i].pdTypeId+'"><a href="good-classify.html?pdTypeId='+list[i].pdTypeId+'"><img src="'+list[i].iconPath+'" alt=""></a><p>'+list[i].pdTypeName+'</p></li>';
            }
            li+='<div class="swiper-slide"><ul class="zwl-goodUl l">'+dataS+'</ul></div>';   
           
        }
        $(".zwl-goodDetail").html(li);
        
    }
    
    //获取附近产品信息
    getData(url,dataProduct,call_func1,async,type);
    
    //获取全部产品类
    getData(url,allProduct,call_func2,async,type);
    var current_Page=1;
    /**
     *滚动加载更多*
     **/
    $(window).scroll(function(){
        totalheight = parseFloat($(window).height()) + parseFloat($(window).scrollTop())-400;
        $(".data-loading").removeClass("hide").addClass("show");
        if((parseFloat($(document).height())-400)==totalheight){ 
            console.log(totalPage+"---"+current_Page);
            if(totalPage>current_Page){
                current_Page+=1;
                var data_p=JSON.stringify({//根据address获取附近产品
                    "code":"getProductList",
                    "version":"1.0",
                    "data":{
                        "areaId":"",
                        "parentPdTypeId":0,
                        "userId":0,
                        "address":address,
                        "currentPage":current_Page,
                        "showCount":show_count,
                        "productName":"",
                        "productTypeId":0
                    }
                });
                getData(url,data_p,call_func1,async,type);    
            }else{
                $(".data-loading").html("已加载全部");
            }
        } 
     });
});
